<?php
class resultController extends ExtendedController
{
    function __construct()
    {
        parent::__construct();
        $this->loadModel('result');
    }
    
    function index()
    {
        $session = Session::getInstance();
        switch($session->get('permission'))
        {
            case 1:
            {
                $this->loadView('admin_result');
//                $this->views['admin_result']->setBlockData($data);
                $this->views['admin_result']->render();
                break;
            }
            
            case 2:
            {
                $data = $this->models['result']->viewResult(2);
                $this->loadView('lecture_result');
                $this->views['lecture_result']->setBlockData($data);
                $this->views['lecture_result']->render();
                break;
            }
            
            case 3:
            {
                $data = $this->models['result']->viewResult(3);
                $this->loadView('student_result');                    
                $this->views['student_result']->setBlockData($data);
                $this->views['student_result']->render();
                break;
            }
        }
    }

    public function search()
    {        
        $session = Session::getInstance();     
        switch($session->get('permission'))
        {
            case 1:
                {
                    if(isset($_POST['submit']))
                    {
                        $this->loadModel('result');
                        $data = $this->models['result']->adminSearch();
                        $this->loadView('admin_result');
                        $this->views['admin_result']->setBlockData($data);
                        $session->set('saved_query', $data);
                        $this->views['admin_result']->render();
                    }
                    else
                    {
                        $this->index();
                    }
                }
                break;

            case 2:
                {
                    $this->loadView('lecture_result');
                    $data = $this->models['result']->lectureSearch();
                    $this->views['lecture_result']->setBlockData($data);
                    $session->set('saved_query',$data);
                    $this->views['lecture_result']->render();
                    break;
                }



            case 3:
                {
                    if(func_num_args() < 2)
                        {$this->index ();return;}
                    $this->loadView('student_result');                    
                    $paramArr = func_get_args();
                    $data = $this->models['result']->studentSearch($paramArr[0], $paramArr[1]);
                    $data['current_action'] = 'search';
                    $data['queryString'] = $paramArr[0] . '/' . $paramArr[1];
                    $this->views['student_result']->setBlockData($data);
                    $this->views['student_result']->render();
                }
                break;
        }
        
    }
    
    public function export($action = NULL)
    {      
        global $_app_config;
        if($action == NULL)
            $action = 'index';
        $this->loadLibrary('DocGenerator');
        $this->loadModel('result');
        $session = Session::getInstance();
        switch($session->get('permission'))
        {
            case '3':
            {
                if($action == 'index')
                {
                    $generator = new DocGenerator();
                    $data = $this->models['result']->viewResult(3);
                    $this->loadView('student_result_export');                    
                    $this->views['student_result_export']->setBlockData($data);
                    $resource = $this->views['student_result_export']->renderBlock($_app_config['masterTemplate']['default_render_block']);
                    $generator->addParagraph($resource);
                    $generator->setDocumentCharset('utf-8');
                    $generator->output($session->get('id') . '_' . $session->get('name') . '_result.doc');
                }
                elseif($action == 'search')
                {
                    $generator = new DocGenerator();
                    $params = func_get_args();
                    $data = $this->models['result']->studentSearch($params[1], $params[2]);
                    $this->loadView('student_result_export');                    
                    $this->views['student_result_export']->setBlockData($data);
                    $resource = $this->views['student_result_export']->renderBlock($_app_config['masterTemplate']['default_render_block']);
                    $generator->addParagraph($resource);
                    $generator->setDocumentCharset('utf-8');
                    $generator->output($session->get('id') . '_' . $session->get('name') . '_result.doc');
                }
                else
                {
                    Utility::error('Chức năng không tồn tại');
                    exit();
                }
                break;
            }

            case 2:
            {
                if($session->isExist('saved_query'))
                {
                    $data = $session->get('saved_query');
                }
                else
                    $data = NULL;
                if($action == 'index' || $action == 'search')
                {
                    $generator = new DocGenerator();
                    $this->loadView('lecture_result_export');
                    $this->views['lecture_result_export']->setBlockData($data);
                    $resource = $this->views['lecture_result_export']->renderBlock($_app_config['masterTemplate']['default_render_block']);
                    $generator->addParagraph($resource);
                    $generator->setDocumentCharset('utf-8');
                    $generator->output($session->get('id') .  '_result.doc');
                }
                else
                {
                    Utility::error('Chức năng không tồn tại');
                    exit();
                }
                break;
            }

            case 1:
            {
                if ($session->isExist('saved_query'))
                {
                    $data = $session->get('saved_query');
                }
                else
                    $data = NULL;
                if ($action == 'index' || $action == 'search')
                {
                    $generator = new DocGenerator();
                    $this->loadView('admin_result_export');
                    $this->views['admin_result_export']->setBlockData($data);
                    $resource = $this->views['admin_result_export']->renderBlock($_app_config['masterTemplate']['default_render_block']);
                    $generator->addParagraph($resource);
                    $generator->setDocumentCharset('utf-8');
                    $generator->output($session->get('id') . '_result.doc');
                }
                else
                {
                    Utility::error('Chức năng không tồn tại');
                    exit();
                }
                break;
            }
        }
    }
}
 
